package com.yaodian100.ec.announcement;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.entity.Option;
import com.yaodian100.ec.announcement.entity.Announcement;
import com.yaodian100.ec.announcement.service.AnnouncementService;
import com.yaodian100.ec.generate.service.GenerateService;
import com.yaodian100.ec.generatepage.web.CateMainProdAdvAction;

/**
 * 公告管理
 * 
 * @author apple.chang
 * 
 */
public class AnnouncementAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 423375117676651364L;

	/** logger */
	private final Logger logger = LoggerFactory.getLogger(CateMainProdAdvAction.class);

	private SimplePager pager = null;
	private AnnouncementService announcementService;
	private DomainService<Menu> menuService;
	@Autowired
	private GenerateService generateService;
	private List<Announcement> annList;
	private Announcement announcement;
	private Menu annTypeMenu;
	private Menu annIsDeleteMenu;
	private Menu annShowTypeMenu;
	private String actionResult;
	private String anndeleteId;
	private String hasurl;
	private String targetURL;
	private String anncontent;
	private String annShowSeq;
	private String annId;
	private String annNameParam;
	private String startDateParam;
	private String startTime;
	private String endDateParam;
	private String endTime;
	private String minDate;
	private String annTypeParam;
	private String isDeleteParam;
	private String showTypeParam;
	private String isHighlight;
	private String adWord;

	/******* 查询页面参数 ********/
	private String sannNameParam;
	private String sstartDateParam;
	private String sstartTime;
	private String sendDateParam;
	private String sendTime;
	private String sminDate;
	private String sannTypeParam;
	private String sisDeleteParam;
	private String sshowTypeParam;

	public void prepare() throws Exception {
		if (null == pager) {
			pager = new SimplePager();
		}

		if (null == annTypeMenu) {
			annTypeMenu = menuService.get("Announcement.Type");
		}
		Map<String, Option> allOptions = this.annTypeMenu.getOptions();
		allOptions.remove(AnnouncementService.ANNOUNCEMENT_TYPE_MEMBER_MESSAGE);
		this.annTypeMenu.setOptions(allOptions);

		if (null == annIsDeleteMenu) {
			annIsDeleteMenu = menuService.get("Announcement.IsDelete");
		}

		if (null == annShowTypeMenu) {
			annShowTypeMenu = menuService.get("Announcement.ShowType");
		}
	}

	/**
	 * 查询公告-for midend-先全秀了
	 * 
	 * @return
	 * @throws CoreException
	 * @throws ParseException
	 */
	public String searchIntranet() throws CoreException, ParseException {
		// search value
		CommonCriteria crit = new CommonCriteria();
		Date now = new Date();
		crit.addLe("startDate", now);
		crit.addGe("endDate", now);
		crit.addEq("isDelete", "N");

		int contentSize = announcementService.getListSize(crit).intValue();
		pager.setPageRecord(20);
		pager.setTotalSize(contentSize);
		annList = announcementService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), crit,
				new String[] { "showSeq desc" });
		return "listIntranet";
	}

	/**
	 * list announcement
	 * 
	 * @return
	 * @throws CoreException
	 * @throws ParseException
	 */
	public String search() throws CoreException, ParseException {
		sminDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getTodayAddDays(-30).getTime());
		// search value
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(sannNameParam)) {
			crit.addRlike("subject", sannNameParam);
		}
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtils.isNotBlank(sstartDateParam)) {
			Date sday = dateFormat.parse(sstartDateParam);
			crit.addGe("startDate", sday);
		}
		if (StringUtils.isNotBlank(sendDateParam)) {
			Date eday = dateFormat.parse(sendDateParam);
			crit.addLe("endDate", eday);
		}
		if (StringUtils.isNotBlank(sannTypeParam)) {
			crit.addEq("type", sannTypeParam);
		} else {
			crit.addIn("type", this.annTypeMenu.getOptions().keySet());
		}

		if (StringUtils.isNotBlank(sisDeleteParam)) {
			crit.addEq("isDelete", sisDeleteParam);
		}
		if (StringUtils.isNotBlank(sshowTypeParam)) {
			Date now = new Date();
			if (sshowTypeParam.equals("WAIT_SHOW")) {
				crit.addGe("startDate", now);
			} else if (sshowTypeParam.equals("SHOW")) {
				crit.addLe("startDate", now);
				crit.addGe("endDate", now);
			} else if (sshowTypeParam.equals("OVERDUE")) {
				crit.addLe("endDate", now);
			}
		}

		int contentSize = announcementService.getListSize(crit).intValue();
		pager.setPageRecord(20);
		pager.setTotalSize(contentSize);
		annList = announcementService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), crit,
				AnnouncementService.DEFAULT_SORT_FOR_HQL);
		return "list";
	}

	public String add() {
		minDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", new Date());
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		startDateParam = dateFormat.format(new Date());
		endDateParam = dateFormat.format(DateUtils.addDays(new Date(), 7));
		startTime = "00:00";
		endTime = "00:00";
		annId = "";
		targetURL = "";
		anncontent = "";
		annNameParam = "";
		annShowSeq = "";
		isHighlight = "N";
		adWord = "";
		return "add";
	}

	public String edit() throws CoreException {
		Object[] obj = announcementService.getAnnouncementById(annId);
		Announcement announcement = (Announcement) obj[0];
		Date tmp = new Date();
		if (announcement.getStartDate().getTime() < System.currentTimeMillis()) {
			tmp = announcement.getStartDate();
		}
		annNameParam = announcement.getSubject();
		minDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", tmp);
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		startDateParam = dateFormat.format(announcement.getStartDate());
		endDateParam = dateFormat.format(announcement.getEndDate());
		dateFormat = new SimpleDateFormat("HH:mm");
		startTime = dateFormat.format(announcement.getStartDate());
		endTime = dateFormat.format(announcement.getEndDate());
		annTypeParam = announcement.getType();
		annShowSeq = announcement.getShowSeq();
		isHighlight = announcement.getIsHighlight();
		targetURL = announcement.getTargetURL();
		setAnncontent(obj[1].toString());
		adWord = announcement.getAdWord();
		if (targetURL != null) {
			hasurl = "Y";
		} else {
			hasurl = "N";
		}
		return "edit";
	}

	/**
	 * 儲存公告
	 * 
	 * @return
	 * @throws ParseException
	 * @throws CoreException
	 */
	public String save() throws CoreException, ParseException {
		logger.info("Announcement subject: " + annNameParam);
		Announcement announcement;
		if (StringUtils.isNotBlank(annId)) {
			announcement = (Announcement) announcementService.getAnnouncementById(annId)[0];
		} else {
			announcement = new Announcement();
			announcement.setIsDelete("N");
		}
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date sday = dateFormat.parse(startDateParam + " " + startTime);
		Date eday = dateFormat.parse(endDateParam + " " + endTime);
		announcement.setSubject(annNameParam);
		announcement.setStartDate(sday);
		announcement.setEndDate(eday);
		announcement.setType(annTypeParam);
		announcement.setShowSeq(annShowSeq);
		announcement.setIsHighlight(isHighlight);
		announcement.setAdWord(adWord);
		if (hasurl.equals("Y")) {
			announcement.setTargetURL(targetURL);
			announcement.setBody(null);
		} else {
			announcement.setTargetURL(null);
			announcement.setBody(anncontent);
		}
		if (null == announcementService.saveOrUpdateAnnouncement(announcement)) {
			if (StringUtils.isNotBlank(annId)) {
				return "edit";
			} else {
				return "add";
			}
		}
		try {
			generateService.generateIdxNews(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		annNameParam = "";
		minDate = "";
		startDateParam = "";
		endDateParam = "";
		startTime = "";
		endTime = "";
		annTypeParam = "";
		annShowSeq = "0";
		targetURL = "";
		adWord = "";
		return "save_ok";
	}

	/**
	 * 刪除公告
	 * 
	 * @param announcementId
	 * @return
	 * @throws CoreException
	 */
	public String deleteAnnouncement() throws CoreException {
		logger.info("delete announcement id: " + anndeleteId);
		if (null == announcementService.deleteAnnouncement(anndeleteId)) {
			actionResult = "delete error!";
		} else {
			actionResult = "success";
		}
		return "announcement_delete";
	}

	/**
	 * 檢視公告
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String view() throws CoreException {
		Object[] obj = announcementService.getAnnouncementById(annId);
		announcement = (Announcement) obj[0];
		annTypeParam = announcement.getTypeName();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		startDateParam = dateFormat.format(announcement.getStartDate());
		endDateParam = dateFormat.format(announcement.getEndDate());
		isHighlight = announcement.getIsHighlight();
		targetURL = announcement.getTargetURL();
		if (StringUtils.isBlank(targetURL)) {
			anncontent = obj[1].toString().replaceAll("\n", "<br/>");
		} else {
			targetURL = "<a href='" + targetURL + "' target='_blank'>" + targetURL + "</a>";
		}
		return "view";
	}

	/**
	 * 檢視公告-for midend
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String viewIntranet() throws CoreException {
		view();
		return "viewIntranet";
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public AnnouncementService getAnnouncementService() {
		return announcementService;
	}

	public void setAnnouncementService(AnnouncementService announcementService) {
		this.announcementService = announcementService;
	}

	public String getMinDate() {
		return minDate;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public List<Announcement> getAnnList() {
		return annList;
	}

	public void setAnnList(List<Announcement> annList) {
		this.annList = annList;
	}

	public String getStartDateParam() {
		return startDateParam;
	}

	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	public String getAnnNameParam() {
		return annNameParam;
	}

	public void setAnnNameParam(String annNameParam) {
		this.annNameParam = annNameParam;
	}

	public Menu getAnnTypeMenu() {
		return annTypeMenu;
	}

	public void setAnnTypeMenu(Menu annTypeMenu) {
		this.annTypeMenu = annTypeMenu;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public String getAnnTypeParam() {
		return annTypeParam;
	}

	public void setAnnTypeParam(String annTypeParam) {
		this.annTypeParam = annTypeParam;
	}

	public String getIsDeleteParam() {
		return isDeleteParam;
	}

	public void setIsDeleteParam(String isDeleteParam) {
		this.isDeleteParam = isDeleteParam;
	}

	public Menu getAnnIsDeleteMenu() {
		return annIsDeleteMenu;
	}

	public void setAnnIsDeleteMenu(Menu annIsDeleteMenu) {
		this.annIsDeleteMenu = annIsDeleteMenu;
	}

	public String getActionResult() {
		return actionResult;
	}

	public String getAnndeleteId() {
		return anndeleteId;
	}

	public void setAnndeleteId(String anndeleteId) {
		this.anndeleteId = anndeleteId;
	}

	public void setActionResult(String actionResult) {
		this.actionResult = actionResult;
	}

	public String getHasurl() {
		return hasurl;
	}

	public void setHasurl(String hasurl) {
		this.hasurl = hasurl;
	}

	public String getTargetURL() {
		return targetURL;
	}

	public void setTargetURL(String targetURL) {
		this.targetURL = targetURL;
	}

	public String getAnncontent() {
		return anncontent;
	}

	public void setAnncontent(String anncontent) {
		this.anncontent = anncontent;
	}

	public String getAnnShowSeq() {
		return annShowSeq;
	}

	public void setAnnShowSeq(String annShowSeq) {
		this.annShowSeq = annShowSeq;
	}

	public String getAnnId() {
		return annId;
	}

	public void setAnnId(String annId) {
		this.annId = annId;
	}

	/**
	 * @return the startTime
	 */
	public String getStartTime() {
		return startTime;
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	/**
	 * @return the endTime
	 */
	public String getEndTime() {
		return endTime;
	}

	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	/**
	 * @return the annShowTypeMenu
	 */
	public Menu getAnnShowTypeMenu() {
		return annShowTypeMenu;
	}

	/**
	 * @param annShowTypeMenu the annShowTypeMenu to set
	 */
	public void setAnnShowTypeMenu(Menu annShowTypeMenu) {
		this.annShowTypeMenu = annShowTypeMenu;
	}

	/**
	 * @return the showTypeParam
	 */
	public String getShowTypeParam() {
		return showTypeParam;
	}

	/**
	 * @param showTypeParam the showTypeParam to set
	 */
	public void setShowTypeParam(String showTypeParam) {
		this.showTypeParam = showTypeParam;
	}

	public String getSannNameParam() {
		return sannNameParam;
	}

	public void setSannNameParam(String sannNameParam) {
		this.sannNameParam = sannNameParam;
	}

	public String getSstartDateParam() {
		return sstartDateParam;
	}

	public void setSstartDateParam(String sstartDateParam) {
		this.sstartDateParam = sstartDateParam;
	}

	public String getSstartTime() {
		return sstartTime;
	}

	public void setSstartTime(String sstartTime) {
		this.sstartTime = sstartTime;
	}

	public String getSendDateParam() {
		return sendDateParam;
	}

	public void setSendDateParam(String sendDateParam) {
		this.sendDateParam = sendDateParam;
	}

	public String getSendTime() {
		return sendTime;
	}

	public void setSendTime(String sendTime) {
		this.sendTime = sendTime;
	}

	public String getSminDate() {
		return sminDate;
	}

	public void setSminDate(String sminDate) {
		this.sminDate = sminDate;
	}

	public String getSannTypeParam() {
		return sannTypeParam;
	}

	public void setSannTypeParam(String sannTypeParam) {
		this.sannTypeParam = sannTypeParam;
	}

	public String getSisDeleteParam() {
		return sisDeleteParam;
	}

	public void setSisDeleteParam(String sisDeleteParam) {
		this.sisDeleteParam = sisDeleteParam;
	}

	public String getSshowTypeParam() {
		return sshowTypeParam;
	}

	public void setSshowTypeParam(String sshowTypeParam) {
		this.sshowTypeParam = sshowTypeParam;
	}

	public String getIsHighlight() {
		return isHighlight;
	}

	public void setIsHighlight(String isHighlight) {
		this.isHighlight = isHighlight;
	}

	public Announcement getAnnouncement() {
		return announcement;
	}

	public void setAnnouncement(Announcement announcement) {
		this.announcement = announcement;
	}

	public String getAdWord() {
		return adWord;
	}

	public void setAdWord(String adWord) {
		this.adWord = adWord;
	}
}